﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using Fsb.Utility;
using Newtonsoft.Json;

namespace VideoCache.Spider.FileFetchers
{
    public class LetvFetcher : IFetcher
    {
        public FetcherResult[] GetFiles(string url)
        {
            string html = HttpUtility.Get(url, Encoding.UTF8);

            string script = StringUtility.SubString(html, "<script>", "</script>");

            string vCode = StringUtility.SubString(script, "&v_code=", "'");

            if (string.IsNullOrEmpty(vCode)) return null;

            string xml = Encoding.UTF8.GetString(Convert.FromBase64String(HttpUtility.UrlDecode(vCode, Encoding.UTF8)));

            LetvData data = SerializableUtility.XmlDeSerializer<LetvData>(xml);

            List<FetcherResult> list = new List<FetcherResult>();

            if (data != null)
            {
                LetvJsonData jsonData = JsonUtility.Object<LetvJsonData>(data.PlayUrl);

                if (jsonData.Dispatch.P350 != null)
                {
                    FetcherResult result = new FetcherResult();
                    result.Resolution = Resolution.Level_1;
                    result.Files.Add(jsonData.Dispatch.P350[0]);

                    list.Add(result);
                }

                if (jsonData.Dispatch.P1000 != null)
                {
                    FetcherResult result = new FetcherResult();
                    result.Resolution = Resolution.Level_2;
                    result.Files.Add(jsonData.Dispatch.P1000[0]);

                    list.Add(result);
                }

                if (jsonData.Dispatch.P720P != null)
                {
                    FetcherResult result = new FetcherResult();
                    result.Resolution = Resolution.Level_3;
                    result.Files.Add(jsonData.Dispatch.P720P[0]);

                    list.Add(result);
                }
            }

            if (list.Count == 0)
            {
                throw new FetcherException("无法获取视频下载地址，视频不存在或已删除。");
            }

            return list.ToArray();
        }

        private class LetvJsonData
        {
            public string[] StoreUrl { get; set; }
            public Dispatch Dispatch { get; set; }
            public Dispatch DispatchBak { get; set; }
            public string Title { get; set; }
            public int Duration { get; set; }
        }

        private class Dispatch
        {
            [XmlElement("350"), JsonProperty("350")]
            public string[] P350 { get; set; }
            [XmlElement("1000"), JsonProperty("1000")]
            public string[] P1000 { get; set; }
            [XmlElement("720p"), JsonProperty("720p")]
            public string[] P720P { get; set; }
        }

        [Serializable]
        [XmlRoot("root")]
        public class LetvData
        {
            [XmlElement("id")]
            public int Id { get; set; }
            [XmlElement("status")]
            public int Status { get; set; }
            [XmlElement("error_msg"), JsonProperty("error_msg")]
            public string Error_Message { get; set; }
            [XmlElement("adp")]
            public string Adp { get; set; }
            [XmlElement("wt")]
            public string Wt { get; set; }
            [XmlElement("newurl")]
            public string NewUrl { get; set; }
            [XmlElement("cid")]
            public int Cid { get; set; }
            [XmlElement("tal")]
            public string Tal { get; set; }
            [XmlElement("duration")]
            public int Duration { get; set; }
            [XmlElement("typeurl")]
            public string TypeUrl { get; set; }
            [XmlElement("playurl")]
            public string PlayUrl { get; set; }
        }
    }
}
